//
//  NewDetailScrollView.m
//  New
//
//  Created by dlios on 15-7-18.
//  Copyright (c) 2015年 yangTao. All rights reserved.
//

#import "NewDetailScrollView.h"

@implementation NewDetailScrollView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatSubView];
        
    }
    return self;
}
-(void)creatSubView{

   
    
    self.view = [[UIView alloc]initWithFrame:CGRectMake(0,-self.frame.size.height/5, self.frame.size.width, self.frame.size.height/5)];
    
    self.view.backgroundColor = [UIColor colorWithRed:125/250.0 green:153/250.0 blue:152/250.0 alpha:1.0];
//    [self addSubview:self.view];
//
    self.title = [[BaseLabel alloc]initWithFrame:CGRectMake(10, 0, self.frame.size.width - 15, self.frame.size.height/5 )];
    self.title.backgroundColor = [UIColor clearColor];
     self.title.font = [UIFont systemFontOfSize:22];
    self.title.numberOfLines = 0;
    [self.view addSubview:self.title];
    
    


}

-(void)setDic:(NSMutableDictionary *)dic{

    if (_dic != dic) {
        _dic = dic;
    }

    
    self.title.text = [dic objectForKey:@"title"];

    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, self.frame.size.width - 5, self.frame.size.height)];//初始化UIWebView
    self.webView.scrollView.contentInset =UIEdgeInsetsMake(self.frame.size.height/5, 0, 0, 0);

    //计算高度
    const CGFloat defaultWebViewHeight = 22.0;
    //reset webview size
    CGRect originalFrame = self.webView.frame;
    self.webView.frame = CGRectMake(originalFrame.origin.x, originalFrame.origin.y, self.frame.size.width, defaultWebViewHeight);
    
    CGSize actualSize = [self.webView sizeThatFits:CGSizeZero];
    if (actualSize.height <= defaultWebViewHeight) {
        actualSize.height = defaultWebViewHeight;
    }
    CGRect webViewFrame = self.webView.frame;
    webViewFrame.size.height = actualSize.height;
    self.webView.frame = webViewFrame;
    [self.webView.scrollView addSubview:self.view];

    
    
    
    [self addSubview:self.webView];
    
    NSString *htmlString = [dic objectForKey:@"content"];//设置UIWebView的内容
    [self.webView loadHTMLString:htmlString baseURL:nil];//加载内容
    self.webView.backgroundColor = [UIColor clearColor];
    self.webView.opaque = NO;
    self.webView.scalesPageToFit = YES;
    
    self.webView.delegate = self;
    
    
    
   
    
    

}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //拦截网页图片  并修改图片大小
    [webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth=1200;" //缩放系数
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "if(myimg.width > maxwidth){"
     "oldwidth = myimg.width;"
     "myimg.width = maxwidth;"
     "myimg.height = myimg.height * (maxwidth/oldwidth);"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
    //修改服务器页面的meta的值
//    NSString *meta = [NSString stringWithFormat:@"document.getElementsByName(\"viewport\")[0].content = \"width=%f, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\"", webView.frame.size.width];
//    [webView stringByEvaluatingJavaScriptFromString:meta];
    
    
    //改变字体大小
    NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '300%'";
    [webView stringByEvaluatingJavaScriptFromString:str];
   //改变字体颜色
    NSString *jsString = [NSString  stringWithFormat:@"document.body.style.color=%@",[UIColor blackColor]];
    [webView stringByEvaluatingJavaScriptFromString:jsString];
    
    
    
}




@end
